let roomAPI           = require('../../API/RoomAPI');
var RoomProto         = require('../../API/RoomProto');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var enumeration       = require('../../Constant/enumeration');
var MJModel           = require('./MJModel');

cc.Class({
	extends: cc.Component,
	properties: {
	},

	onLoad: function () {
		let data = this.dialogParameters;
		this.init(data.userArray, data.creator);
		if (!this.dialogParameters.isUnionCreate || !this.dialogParameters.isBureauFull) {
			cc.find('ButtonContinue', this.node).active = false;
		}
		if (data.hongBaoList && data.hongBaoList[MJModel.getMyChairID()] >= 0) {
			Global.DialogManager.createDialog("Game/GameHongBaoDialog", {
				score: data.hongBaoList[MJModel.getMyChairID()],
				hongBaoList: data.hongBaoList,
			});
		}
	},

	init: function (userArray, creator) {
		let winMostArray = [0];
		let loseMostArray = [0];
		for (let i = 1; i < userArray.length; ++i) {
			if (userArray[i].winScore > userArray[winMostArray[0]].winScore) {
				winMostArray = [i];
			}
			else if (userArray[i].winScore == userArray[winMostArray[0]].winScore) {
				winMostArray.push(i);
			}
			if (userArray[i].winScore < userArray[loseMostArray[0]].winScore) {
				loseMostArray = [i];
			}
			else if (userArray[i].winScore == userArray[loseMostArray[0]].winScore) {
				loseMostArray.push(i);
			}
		}
		if (winMostArray.length == userArray.length) {
			winMostArray = [];
		}
		if (loseMostArray.length == userArray.length) {
			loseMostArray = [];
		}

		for (let i = 0; i < userArray.length; ++i) {
			let data = userArray[i];
			let item = cc.instantiate(cc.find('Item', this.node));
			item.parent = cc.find('Layout', this.node);
			item.active = true;
			cc.find('name', item).getComponent(cc.Label).string = data.nickname;
			let head = cc.find('head', item);
			if (data.winScore >= 0) {
				cc.find('win', item).getComponent(cc.Label).string = '+'+parseInt(data.winScore);
				cc.find('lose', item).active = false;
				if (winMostArray.indexOf(i) != -1 && loseMostArray.indexOf(i) == -1) {
					cc.find('winmost', item).active = true;
				}
			}
			else {
				cc.find('lose', item).getComponent(cc.Label).string = parseInt(data.winScore);
				cc.find('win', item).active = false;
				if (loseMostArray.indexOf(i) != -1 && winMostArray.indexOf(i) == -1) {
					cc.find('losemost', item).active = true;
				}
			}
			Global.CCHelper.updateSpriteFrame(data.avatar, head.getComponent(cc.Sprite));
			cc.find('hulabel', item).getComponent(cc.Label).string = data.huCount;
			cc.find('gongganglabel', item).getComponent(cc.Label).string = data.gongGangCount;
			cc.find('anganglabel', item).getComponent(cc.Label).string = data.anGangCount;
			cc.find('malabel', item).getComponent(cc.Label).string = data.maCount;
		}

		let createrNode = cc.find('Creater', this.node);
		if (creator) {
			let name = cc.find('name', createrNode);
			let id = cc.find('id', createrNode);
			let head = cc.find('head', createrNode);
			name.getComponent(cc.Label).string = creator.nickname;
			id.getComponent(cc.Label).string = creator.uid;
			Global.CCHelper.updateSpriteFrame(creator.avatar, head.getComponent(cc.Sprite));
		}
		else {
			createrNode.active = false;
		}
	},

	onButtonClick: function (event, param) {
        Global.AudioManager.playCommonSoundClickButton();
		if (param == 'back') {
			if (this.dialogParameters.isUnionCreate) {
				Global.DialogManager.destroyDialog('Game/Majiang/MJAllResult');
				if (this.dialogParameters.roomDismissReason != enumeration.gameRoomDismissReason.UNION_OWNER_DISMISS) {
					Global.NetworkManager.notify(RoomMessageRouter, RoomProto.userLeaveRoomNotify());
				}
				else {
					this.dialogParameters.enterHall();
				}
			}
			else {
				Global.DialogManager.destroyDialog('Game/Majiang/MJAllResult');
				this.dialogParameters.enterHall();
			}
		}
		else if(param == 'share') {
			let imgPath = Global.CCHelper.capScreen(this.node);
			Global.PlatformHelper.wxShareImg(imgPath, 0);
		}
		else if(param == 'continue') {
			Global.DialogManager.destroyDialog('Game/Majiang/MJAllResult'); 
			roomAPI.roomMessageNotify(RoomProto.getRoomSceneInfoNotify()); 
		}
	},
});

